﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Trim
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void TextBoxes_TextChanged(object sender, EventArgs e)
        {
            if (FirstNameBox.Text.Length > 0
                && LastNameBox.Text.Length > 0
                && PasswordBox.Text.Length > 0
                && LocationBox.Text.Length > 0
                && LoginURIBox.Text.Length > 0)
            {
                LoginButton.Enabled = true;
            }
            else
            {
                LoginButton.Enabled = false;
            }
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void Login()
        {
            Program.client.SL = new OpenMetaverse.GridClient();
            Program.client.SL.Settings.LOGIN_SERVER = LoginURIBox.Text;
            Program.client.LoginParams = Program.client.SL.Network.DefaultLoginParams(FirstNameBox.Text, LastNameBox.Text, PasswordBox.Text, "Trim", "Trim 0.1");

            string start = LocationBox.Text;
            if (start.ToLower() == "my home") start = "home";
            else if (start.ToLower() == "last location") start = "last";
            else
            {
                try { start = OpenMetaverse.NetworkManager.StartLocation(start, 10, 246, 4096); }
                catch { start = ""; }
                if (start == "")
                {
                    MessageBox.Show("Invalid start location.");
                    return;
                }
            }
            Program.client.LoginParams.Start = start;

            this.Hide();
            Program.client.LoggingInFormInstance = new LoggingInForm();
            Program.client.LoggingInFormInstance.Show();
        }

        private void PressEnterKey(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (LoginButton.Enabled == true)
                {
                    e.Handled = true;
                    Login();
                }
            }
        }
    }
}
